﻿using System;
using System.Collections.Generic;
using AllDeal.DataAccess;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace AllDeals.CollectorEngine.HotDeal {
    public class HotDealAnalyzer {
        public const string HOME_URL = "http://www.hotdeal.vn";
        public const string HCM_URL = "http://www.hotdeal.vn/ho-chi-minh/";
        public const string HANOI_URL = "http://www.hotdeal.vn/ha-noi/";
        public List<Product> GetProducts(string url) {
            var web = new HtmlWeb();
            HtmlDocument doc = web.Load(url);
            var gridNode = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'central-content')]");
            var pNodes = gridNode.SelectNodes(".//div[contains(@class,'product-item')]");
            List<Product> products = new List<Product>(pNodes.Count);
            foreach (HtmlNode pNode in pNodes) {
                Product p = GetProduct(pNode);
                if (p != null) {
                    products.Add(p);
                }
            }
            return products;
        }
        private Product GetProduct(HtmlNode node) {
            Product p = new Product();
            HtmlNode buyNo = node.SelectSingleNode(".//div[contains(@class,'buy_number')]/span");

            if (buyNo != null) {
                int no;
                var bString = buyNo.InnerText.Trim();
                if (int.TryParse(bString, out no)) {
                    p.BuyNo = no;
                }
                p.BuyNo = no;
            }
            HtmlNode aThumb = node.SelectSingleNode(".//div[contains(@class,'thumb')]/a");
            if (aThumb != null) {
                p.OriginalUrl = HOME_URL + aThumb.GetAttributeValue("href", string.Empty);
                HtmlNode img = node.SelectSingleNode(".//div[contains(@class,'thumb')]/a/img");
                p.ThumbImg = img.GetAttributeValue("data-original", string.Empty);
            }
            HtmlNode title = node.SelectSingleNode("./div[contains(@class,'title')]/h2/a");
            if (title != null) {
                p.Title = title.InnerText.Trim();
            }
            HtmlNode sellPrice = node.SelectSingleNode("./div[contains(@class,'title')]/span[contains(@class,'sell-price')]");
            if (sellPrice != null) {
                p.Price = TryParsePrice(sellPrice.InnerText.Trim());

            }
            HtmlNode orginnalPrice = node.SelectSingleNode("./div[contains(@class,'title')]/span[contains(@class,'original-price')]");
            if (orginnalPrice != null) {
                p.OrginalPrice = TryParsePrice(orginnalPrice.InnerText.Trim());
            }
            p.Discount = Convert.ToDouble((p.OrginalPrice - p.Price)/(p.OrginalPrice == 0 ? 1 : p.OrginalPrice));
            p.OriginalSite =HOME_URL;
            p.PlaceId = 1;// TODO: replace
            p.CategoryId = 1;// TODO: replace
            return p;
        }
        private decimal TryParsePrice(string price) {
            price = price.Substring(0, price.Length - 1);
            price = price.Replace(".", "");
            decimal pDecimal;
            if (!decimal.TryParse(price, out pDecimal)) {
                pDecimal = 0;
            }
            return pDecimal;
        }
    }
}
